package org.groupg.practice;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedGraph;
import org.jgrapht.traverse.TopologicalOrderIterator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DAGReverseTraversal {
    public static void main(String[] args) {
        // 创建一个有向图
        Graph<String, DefaultWeightedEdge> graph = new SimpleDirectedGraph<>(DefaultWeightedEdge.class);

        // 添加节点和边
        graph.addVertex("A");
        graph.addVertex("B");
        graph.addVertex("C");
        graph.addVertex("D");
        graph.addVertex("E");
        graph.addVertex("F");
        graph.addVertex("G");
        graph.addVertex("H");
        graph.addEdge("A", "B");
        graph.addEdge("B", "C");
        graph.addEdge("B", "D");

        // 使用拓扑排序进行遍历，并反转结果以实现从叶节点到根节点的遍历
        List<String> topologicalOrder = new ArrayList<>();


        new TopologicalOrderIterator<>(graph).forEachRemaining(topologicalOrder::add);
//        Collections.reverse(topologicalOrder); // 反转列表以实现逆序遍历

        // 输出遍历结果
        topologicalOrder.forEach(a -> {

        });
    }
}